package com.alit.ibot.manager.attachement.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alit.ibot.manager.attachement.data.IdUrlData;
import com.alit.ibot.manager.attachement.db.UrlMapData;
import com.alit.ibot.manager.attachement.util.FJSONUtil;

/**
 * @author litong
 * @date 2019年6月20日_下午3:30:53
 * @version 1.0
 * @desc
 */
public class UrlManagerService {

  /**
   * 返回重定向后的url
   * 
   * @param para0
   * @return
   */
  public String get(String id) {
    if (id == null) {
      return "/index.html";
    }
    int intId = Integer.parseInt(id);
    String retval = null;
    retval = UrlMapData.get(intId);
    return retval;
  }

  public JSONObject list() {
    JSONObject retval = new JSONObject();
    retval.put("total", UrlMapData.size());
    JSONArray rows = FJSONUtil.mapToJSONArray(UrlMapData.getMap());
    retval.put("rows", rows);
    return retval;
  }

  public JSONObject update(IdUrlData data) {
    UrlMapData.put(data);
    JSONObject success = new JSONObject();
    success.put("success", true);
    return success;
  }

  public JSONObject paginate(int page, int rows) {
    JSONObject retval = new JSONObject();
    int start = (page - 1) * rows;
    retval.put("total", UrlMapData.size());
    JSONArray rowsData = FJSONUtil.mapToJSONArray(UrlMapData.getMap(), start, start + rows);
    retval.put("rows", rowsData);
    return retval;
  }

  public JSONObject destory(Integer id) {
    UrlMapData.remove(id);
    JSONObject success = new JSONObject();
    success.put("success", true);
    return success;
  }
}
